import keyboard

from .interact_win32 import mouse_move
import mouse
from time import sleep


class _MouseButtons:
    LEFT = left = 'left'
    RIGHT = right = 'right'
    MIDDLE = middle = 'middle'
    WHEEL = wheel = 'wheel'
    X = x = 'x'
    X2 = x2 = 'x2'


class Mouse:
    buttons = _MouseButtons

    def __init__(self, use_rel_move=False):
        self.use_rel_move = use_rel_move

    @property
    def position(self):
        position = mouse.get_position()
        return position

    @position.setter
    def position(self, xy):
        if self.use_rel_move:
            x0, y0 = self.position
            x1, y1 = xy
            self.move_r(x1 - x0, y1 - y0)
        else:
            mouse.move(*xy)

    def move_r(self, dx, dy, interval=0, times=10):  # move_relative
        if interval:
            _interval = interval / times
            x_a = dx // times
            x_b = dx % times

            y_a = dy // times
            y_b = dy % times
            for i in range(times):
                _dx = x_a
                _dy = y_a
                if i == times - 1:
                    _dx += x_b
                    _dy += y_b
                mouse_move(_dx, _dy)
                sleep(_interval)
        else:
            mouse_move(dx, dy)

    def move_to(self, x, y, interval=0, times=10):
        if not self.use_rel_move:
            mouse.move(x, y)
            return

        if interval:
            x0, y0 = self.position
            self.move_r(x - x0, y - y0, interval=interval, times=times)
        else:
            self.position = x, y

    def is_pressed(self, button: str = buttons.left):
        return mouse.is_pressed(button)

    def press(self, button: str = buttons.left):
        mouse.press(button)

    def release(self, button: str = buttons.left):
        mouse.release(button)

    def click_left(self, x=None, y=None, interval=0):
        self.click(button=self.buttons.left, x=x, y=y, interval=interval)

    def click_right(self, x=None, y=None, interval=0):
        self.click(button=self.buttons.right, x=x, y=y, interval=interval)

    def click_middle(self, x=None, y=None, interval=0):
        self.click(button=self.buttons.middle, x=x, y=y, interval=interval)

    def click(self, button: str = buttons.left, x: (None, int) = None, y: (None, int) = None, interval=0):
        if None not in (x, y):
            self.move_to(x, y)

        self.press(button)
        if interval:
            sleep(interval)
        self.release(button)
        return 1


class Key:
    def press(self, key):
        return keyboard.press(key)

    def release(self, key):
        return keyboard.release(key)

    def press_and_release(self, key):
        return keyboard.press_and_release(key)


kk = Key()
ms = Mouse()


def get_public_attrs(cls_or_instance):
    _attrs = cls_or_instance.__dict__ if hasattr(cls_or_instance, '__dict__') else cls_or_instance.__dir__()
    attrs = [i for i in _attrs if not i.startswith('_')]
    return attrs


if __name__ == '__main__':
    ms.move_r(100, 100, interval=10, times=10)

